// index.js
import { createRouter, createWebHistory } from 'vue-router'

// 引入页面组件
import Index from '@/views/Index.vue'
import UserIndex from "@/views/UserIndex.vue"
import Login from '@/views/Login.vue'
import UserLogin from '@/views/UserLogin.vue'
import Admin from '@/views/Admin.vue'
import Course from '@/views/Course.vue'
import Coach from "@/views/Coach.vue"
import Chart from "@/views/Chart.vue"
import Role from "@/views/Role.vue"
import User from "@/views/User.vue"
import UserReserve from "@/views/UserReserve.vue"
import Permission from "@/views/Permission.vue"
import Reserve from "@/views/Reserve.vue"
import Comment from "@/views/Comment.vue"

// 创建路由器
const router = createRouter({
    history: createWebHistory(),
    routes: [
        { path: '/login', component: Login },
        { path: '/user-login', component: UserLogin },
        { path: '/userregister', component: UserRegister },

        // 管理员端布局
        {
            path: '/',
            component: Index,
            children: [
                { path: '', component: Chart }, // 默认首页
                { path: 'admin', component: Admin },
                { path: 'course', component: Course },
                { path: 'coach', component: Coach },
                { path: 'chart', component: Chart },
                { path: 'role', component: Role },
                { path: 'user', component: User },
                { path: 'reserve', component: Reserve },
                { path: 'permission', component: Permission },
                { path: 'comment', component: AdminComment }
            ]
        },

        // 普通用户端布局
        {
            path: '/userindex',
            component: UserIndex,
            children: [
/*
                { path: '', component: Introduction }, // 默认首页
*/
                { path: 'usercourse', component: UserCourse },
                { path: 'introduction', component: UserIntroduction },
                { path: 'userreserve', component: UserReserve },
                { path: 'userchart', component: UserChart },
                { path: 'myvip', component: MyVip },
                { path: 'comment', component: Comment }
            ]
        }
    ]
})

// 全局前置守卫
import { useTokenStore } from '@/store/token.js'
import UserCourse from "@/views/UserCourse.vue";
import UserIntroduction from "@/views/UserIntroduction.vue";
import UserChart from "@/views/UserChart.vue";
import UserRegister from "@/views/UserRegister.vue";
import MyVip from "@/views/MyVip.vue";
import AdminComment from "@/views/AdminComment.vue";
let whiteList = ['/login', '/user-login','/userregister']
router.beforeEach((to, from, next) => {
    const tokenStore = useTokenStore()
    const token = tokenStore.token
    if (!whiteList.includes(to.path) && !token) {
        next('/login')
    } else {
        next()
    }
})

export default router
